﻿Public Class NguoiDungDAO
   Private connect As Connect
    Private sqlstr As String
    Public Sub New()
        connect = New Connect
    End Sub

    Public Function CapNhat(ByVal emp As NguoiDungDTO, ByVal flag As Boolean)
        Dim ten As String = emp.username
        Dim matkhau As String = emp.pass
        Dim manguoidung As String = emp.MaUser

        '-------------------------------------------'
        If flag = True Then
            sqlstr = "INSERT NGUOIDUNG (username, pass,MaUser )" & _
                        " VALUES('" & ten & "', '" & matkhau & "','" & manguoidung & "')"
        Else
            sqlstr = "UPDATE NGUOIDUNG SET  username  = '" & ten & _
                                        "', pass   = '" & matkhau & _
                                         "', MaUser   = '" & manguoidung & _
                                    "' WHERE MaUser  = '" & manguoidung & "'"

        End If
        Return connect.Execute(sqlstr)
    End Function
    Public Function XoaNguoiDung(ByVal emp As NguoiDungDTO)
        Dim manguoidung As String = emp.MaUser()
        sqlstr = "DELETE NGUOIDUNG WHERE MaUser = '" & manguoidung & "'"
        Return connect.Execute(sqlstr)
    End Function

    Public Function GetData() As List(Of NguoiDungDTO)
        Dim i As Integer
        Dim ten, matkhau, manguoidung As String
        Dim ndDTO As NguoiDungDTO
        Dim ndList As New List(Of NguoiDungDTO)
        Dim dt As New DataTable
        dt = connect.GetData("select * from NGUOIDUNG")
        For i = 0 To dt.Rows.Count() - 1

            ten = dt.Rows(i)("username").ToString
            matkhau = dt.Rows(i)("pass").ToString
            manguoidung = dt.Rows(i)("MaUser").ToString
            ndDTO = New NguoiDungDTO(ten, matkhau, manguoidung)
            ndList.Add(ndDTO)
        Next
        Return ndList
    End Function
End Class
